package com.gyj.user.controller;

import com.gyj.common.CommonResult;
import com.gyj.user.model.User;
import com.gyj.user.service.UserService;
import com.gyj.user.service.impl.UserRedis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Gao on 2018/5/17.
 */
@Api(tags = "用户信息接口")
@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserController {

    protected static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private UserRedis userRedis;

    @ApiOperation(value = "初始化数据，存在mysql")
    @GetMapping("/initData")
    public String initData() {
        userService.initData();

        return "success";
    }

    @ApiOperation(value = "根据loginName获取user, 从mysql中查")
    @GetMapping("/getUserByLoginName/{loginName}")
    public User getUserByName(@PathVariable String loginName) {
        User user = userService.findByLoginName(loginName);

        return user;
    }

    @ApiOperation(value = "存储数据到redis")
    @GetMapping("/initRedisData")
    public String initRedisData() {
        userService.redisInitData();
        return "success";
    }

    @ApiOperation(value = "根据用户名获取用户列表添加到redis中，可多个")
    @GetMapping("/addRedisByNickName")
    public String addRedisByNickName(@RequestParam("nickName") String nickName) {
        userService.addRedisByNickName(nickName);

        return "success";
    }

    @ApiOperation(value = "根据用户昵称获取对象数据，可能多个，如果有直接从redis中取数据，没有则从mysql中然后保存到redis中")
    @GetMapping("/getUserListByNickName")
    public List<User> getUserListByNickName(@RequestParam("nickName") String nickName) {
        List<User> userListByNickName = userService.getUserListByNickName(nickName);

        return userListByNickName;
    }

    @ApiOperation(value = "添加用户昵称到redis, hash数据")
    @GetMapping("/addUserListByNickNameHash")
    public CommonResult<Boolean> addUserListByNickNameHash(@RequestParam("nickName") String nickName) {

        CommonResult<Boolean> result = null;
        result = userService.addUserListByNickNameHash(nickName);

        return result;
    }

    @ApiOperation(value = "根据id获取用户,优先从redis中取, Id为hash的key")
    @GetMapping("/getUserByIdHash")
    public User getUserByIdHash(@RequestParam("id") Integer id) {
        User user = userService.getUserByIdHash(id);

        return user;
    }

    @ApiOperation(value = "更新用户信息后要设置缓存同步，即删除缓存")
    @PostMapping("/updateUser")
    public CommonResult<Boolean> updateUser(@RequestBody User user) {
        CommonResult<Boolean> result = null;
        result = userService.updateUser(user);

        return result;
    }

    @ApiOperation(value = "根据用户id删除用户信息后要设置缓存同步，即删除缓存")
    @GetMapping("/deleteUserById")
    public CommonResult<Boolean> deleteUserById(@RequestParam("id") Integer id) {
        CommonResult<Boolean> result = null;
        result = userService.deleteUserById(id);

        return result;
    }

    @ApiOperation(value = "根据key在redis删除对应的value")
    @GetMapping("/deleteRedisValueByKey")
    public String deleteRedisValueByKey(@RequestParam("key") String key) {
        userRedis.deleteByKey(key);

        return "success";
    }

    @ApiOperation(value = "根据用户名拼接Key在redis中查找value")
    @GetMapping("/getUserRedisByLoginName/{loginName}")
    public User getUserRedisByLoginName(@PathVariable String loginName) {
        User user = userService.getUserRedis(loginName);

        return user;
    }

    @ApiOperation(value = "根据id查找用户, 还没测试好")
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable int id) {
        User user = userService.getUserById(id);

        return user;
    }
}
